package view;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.paint.Color;
import javafx.stage.StageStyle;
import model.dao.Database;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class AddGroupView extends Window {

    private OnLineView onLineView;

    private Vector<GMItemView> gmItemViews;

    private ListView selectList;
    private ListView selectedList;

    public AddGroupView(OnLineView onLineView) throws IOException {
        root = FXMLLoader.load(getClass().getClassLoader().getResource("addGroup.fxml"));
        Scene scene = new Scene(root, 600, 400);
        scene.setFill(Color.TRANSPARENT);
        setScene(scene);
        initStyle(StageStyle.TRANSPARENT);
        setResizable(false);
        setTitle("AddGroup");

        this.onLineView = onLineView;
        init();
        move();
        quit();
        minimize();
    }

    private void init() {

        selectList = (ListView) findViewById("selectList");
        selectedList = (ListView) findViewById("selectedList");

        fillSelectedList();
    }

    private void fillSelectedList() {
        Database database = new Database();
        database.connect();
//        String sql = "select account, nickname, portrait from `user` where user_id = ?";
        String sql = "select friend_account from `friends` where your_id = ? and del = 0";
        List<String> accounts = new ArrayList<>();
        try {
            ResultSet resultSet = database.execResult(sql, String.valueOf(onLineView.getId()));
            while (resultSet.next()) {
                String account = resultSet.getString("friend_account");
                accounts.add(account);
            }
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        for (String account : accounts) {
            String sql1 = "select account, nickname, portrait from `user` where account = ?";
            String user_account = null;
            String user_nickname = null;
            String user_portrait = null;
            try {
                ResultSet resultSet1 = database.execResult(sql1, account);
                if (resultSet1.next()) {
                    user_account = resultSet1.getString("account");
                    user_nickname = resultSet1.getString("nickname");
                    user_portrait = resultSet1.getString("portrait");
                }
                resultSet1.close();
                String name = user_nickname + "(" + user_account + ")";
                GMItemView itemView = new GMItemView(name, user_portrait);

                selectList.getItems().add(itemView.getPane());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public OnLineView getOnLineView() {
        return onLineView;
    }

    @Override
    public void quit() {
        ((Button)findViewById("quit")).setOnAction(event -> {
            close();
        });
    }

    @Override
    public void minimize() {
        ((Button)findViewById("min")).setOnAction(event -> {
            setIconified(true);
        });
    }
}
